import {
  call, put, all, takeLatest
} from 'redux-saga/effects';
import api from '@API'
import {
  GET_MY_COUPONS,
  GET_CONSULTATION_AND_RATING
} from '@ACTIONS/consultation'

const getMyCoupons = function* getMyCoupons(action) {
  try {
    const {
      payload: {
        id, couponMessageId, totalCost, eType, eDate
      } = {}
    } = action
    const params = {
      rId: id, ref: couponMessageId, money: totalCost, eType
    }
    eDate && (params.eDate = eDate)
    const data = yield call(api.getMyCoupons, params)
    yield put({ type: GET_MY_COUPONS.SUCCESS, data })
  } catch (error) {
    yield put({ type: GET_MY_COUPONS.FAILURE, error })
  }
}

const getConsultationWithRating = function* getConsultationWithRating(action) {
  try {
    const { payload } = action
    const consulAndRating = yield call(api.getConsultationDetail, payload)
    yield put({ type: GET_CONSULTATION_AND_RATING.SUCCESS, consulAndRating })
  } catch (error) {
    yield put({ type: GET_CONSULTATION_AND_RATING.FAILURE, error })
  }
}

export default function* rootSaga() {
  yield all([
    takeLatest(GET_MY_COUPONS.REQUEST, getMyCoupons),
    takeLatest(GET_CONSULTATION_AND_RATING.REQUEST, getConsultationWithRating),
  ])
}
